package cn.edu.fudan.rule;

import cn.edu.fudan.tools.GetConfig;
import cn.edu.fudan.type.Config;
import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.PAMAPDimension;
import cn.edu.fudan.type.WaveletPerOrder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 80494 on 2017/4/3.
 */
public class WaveletPresentation {
    private static List<List<Long>> timepoints;
    private static String subject = "subject101";
    private static PAMAPDimension dimension = PAMAPDimension.z_ankle;
    public static void main(String[] args) throws Exception {

        Config config = new GetConfig().getConfig();

        String path = config.getPath();

        /*String filePath = path + subject + "_extract";
        ReadData readData = new ReadData();
        List<DataItem> rawdatas = readData.readPAMAPDataFromFile(filePath,
                true, dimension);

        String trainPath = path + subject + "\\train.txt";
        timepoints = readData.readTimePoint(trainPath, true);*/
        ArrayList<DataItem> rawdatas = new ArrayList<>();
        ArrayList<Double>  dataonly = new ArrayList<>();
        ArrayList<Double>  zeroarray = new ArrayList<>();
        ArrayList<Integer>  zerointarray = new ArrayList<>();


        DataItem test =new DataItem();
        for(int i=0;i<10;i++){  //generate the test number series
            //rawdatas.get(i).setValue(Math.abs(Math.random()));
            test.setValue(Math.abs(0.5));
            rawdatas.add(i,test);

        }
        int maxorder = (int)Math.ceil(Math.log(rawdatas.size())/Math.log(2)); //get the possible max order
        System.out.println();

        for(int i=0;i<Math.pow(2,maxorder);i++){ // fill 0 when the number of series points doesnt reach the 2^n
            zeroarray.add(i,0.0);
            zerointarray.add(i,0);
            //if(rawdatas.get(i).toString().length()!=0 && rawdatas.get(i).toString()!= null){
            if (i<rawdatas.size()){
                dataonly.add(i,rawdatas.get(i).getValue());
            }
            else {
                dataonly.add(i,0.0);
            }
        }
        int t=dataonly.size();
        int j=0;
        int q=1;

       WaveletPerOrder waveletpre = new WaveletPerOrder();//Initial waveletpresetation
        //WaveletPerOrder waveletpre = new WaveletPerOrder(maxorder,t);//Initial waveletpresetation

        waveletpre.addOrder(j,j);
        waveletpre.addSeries(dataonly,0);    //
        waveletpre.addCoeff(zeroarray,0);
        waveletpre.addDseries(zerointarray,0);
        while (q*2!=t){  //generate waveletpresentation
            j++;

            waveletpre.getOrder().add(j,j);
            waveletpre.getSeries().add(j,waveletpre.SeriesToSeries(waveletpre.getSeries().get(j-1)));
            waveletpre.getCoeff().add(j,waveletpre.SeriesToCoeff(waveletpre.getSeries().get(j-1)));
            waveletpre.getDseries().add(j,waveletpre.SeriesToDseries(waveletpre.getCoeff().get(j),100,0.0,1.0)); //有问题
            q=q*2;
        }
        System.out.println(waveletpre.getSeries().get(0).toString());
        System.out.println(waveletpre.getSeries().get(1).toString());
        System.out.println(waveletpre.getCoeff().get(1).toString());
        System.out.println(waveletpre.getDseries().get(1).toString());
    }

    private List<DataItem> ZNorm(List<DataItem> data){ //Z标准化
        List<DataItem> normdata = new ArrayList<>();
        double sum = 0;
        double squaresum = 0;
        double average = 0;
        double srd = 0;
        if (data.size()==0){
            return data;
        }
        for (int i = 0; i < data.size(); i++) {
            sum += data.get(i).getValue();
        }
        average = sum / data.size();
        for (int i = 0; i < data.size(); i++) {
            squaresum = (data.get(i).getValue() - average) * (data.get(i).getValue() - average);
        }
        srd = Math.sqrt(squaresum) / (data.size() - 1);
        normdata.addAll(data); //初始化
        for (int i = 0; i < data.size(); i++) {
            double t = (data.get(i).getValue()-average)/srd;
            normdata.get(i).setValue((t));
            normdata.get(i).setTimestamp(data.get(i).getTimestamp());

        }
        return normdata;
    }

}
